package cn.kys.modules.system.controller;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class Test {

    public static void main(String[] args) {

        List<Map> list=new ArrayList<>();
        Map map=new HashMap();
        map.put("lat",80);
        map.put("lon",40);
        map.put("POIID",113);
        map.put("NAMEC","sss");
        list.add(map);
        Map map2=new HashMap();
        map2.put("lat",81);
        map2.put("lon",41);
        map2.put("POIID",112);
        map2.put("NAMEC","aaa");
        list.add(map2);
        Map map4=new HashMap();
        map4.put("lat",84);
        map4.put("lon",45);
        map4.put("POIID",116);
        map4.put("NAMEC","aass");
        list.add(map4);

        Map map5=new HashMap();
        map5.put("lat",85);
        map5.put("lon",47);
        map5.put("POIID",14);
        map5.put("NAMEC","aass");
        list.add(map5);

        Map map6=new HashMap();
        map6.put("lat",87);
        map6.put("lon",50);
        map6.put("POIID",116);
        map6.put("NAMEC","aass");
        list.add(map6);
        Double.parseDouble("");
       /* createPointShp(list,"D:\\pro_test_data\\geotoolsShp\\1.shp");
        createLineShp(list,"D:\\pro_test_data\\geotoolsShp\\2.shp");*/
    }

    private static void createPointShp(List<Map> list, String targetFilePath){
        try {
            File file = new File(targetFilePath);
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            tb.setCRS(DefaultGeographicCRS.WGS84);
            tb.setName("shapefile");
            tb.add("the_geom", Point.class);
            tb.add("POIID", Long.class);
            tb.add("NAMEC", String.class);
            tb.add("lon", Double.class);
            tb.add("lat", Double.class);
            ds.createSchema(tb.buildFeatureType());
            ds.setCharset(Charset.forName("utf-8"));
            //设置Writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
                    ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            //写入数据
            for (Map map1 : list) {
                SimpleFeature feature = writer.next();
                feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(Double.parseDouble(map1.get("lon").toString()), Double.parseDouble(map1.get("lat").toString()))));
                feature.setAttribute("POIID", map1.get("POIID"));
                feature.setAttribute("NAMEC", map1.get("NAMEC"));
                feature.setAttribute("lon", map1.get("lon"));
                feature.setAttribute("lat", map1.get("lat"));
                writer.write();
            }
            writer.close();
            ds.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createLineShp(List<Map> list, String targetFilePath) {
        try {
            File file = new File(targetFilePath);
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            tb.setCRS(DefaultGeographicCRS.WGS84);
            tb.setName("shapefile");
            tb.add("the_geom", LineString.class);
            tb.add("POIID", Long.class);
            tb.add("NAMEC", String.class);
            ds.createSchema(tb.buildFeatureType());
            ds.setCharset(Charset.forName("utf-8"));
            //设置Writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            //写入数据
            List<Coordinate> coordinates = new ArrayList<>();
            for (Map map1 : list) {
                coordinates.add(new Coordinate(Double.parseDouble(map1.get("lon").toString()), Double.parseDouble(map1.get("lat").toString())));
            }
            LineString lineString = new GeometryFactory().createLineString(coordinates.toArray(new Coordinate[0]));
            SimpleFeature feature = writer.next();
            feature.setAttribute("the_geom", lineString);
            feature.setAttribute("POIID", "multi_line");
            feature.setAttribute("NAMEC", "multi_line");
            writer.write();
            writer.close();
            ds.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
